package ua.tyran53red.contacts.user;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class UserImp {
	public HashMap<Integer, UserObject> readXML() {
		HashMap<Integer, UserObject> map = new HashMap<Integer, UserObject>();
		try {
			File file = new File("data/contacts_data/user_list/user_list_test.xml");
			int counter = 0;
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory
					.newDocumentBuilder();
			Document document = documentBuilder.parse(file);
			NodeList nodeList = document.getElementsByTagName("user");
			for (int s = 0; s < nodeList.getLength(); s++) {
				Node node = nodeList.item(s);
				Element userElement = (Element) node;
				NodeList firstNameNodeList = userElement
						.getElementsByTagName("first-name");
				Element firstNameElement = (Element)firstNameNodeList.item(0);
				NodeList valuefirstNameNodeList = firstNameElement.getChildNodes();
				NodeList lastNameNodeList = userElement
						.getElementsByTagName("last-name");
				Element lastNameElement = (Element) lastNameNodeList.item(0);
				NodeList valuelastNameNodeList = lastNameElement.getChildNodes();
				NodeList statusNodeList = userElement
						.getElementsByTagName("status");
				Element statusElement = (Element) statusNodeList.item(0);
				NodeList valueStatusNodeList = statusElement.getChildNodes();
				map.put(counter++,
						new UserObject(((Node) valuefirstNameNodeList.item(0))
								.getNodeValue(), ((Node) valuelastNameNodeList
								.item(0)).getNodeValue(),
								((Node) valueStatusNodeList.item(0))
										.getNodeValue()));
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}
}
